using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using TmxLib;
using TmxUtil;

namespace TmxMerge
{
    class Program
    {
        static void Main(string[] args)
        {
            Args a = CommandLineParser<Args>.Parse(args);
            if (a == null)
            {
                Console.WriteLine(string.Join(",", args));
                Console.WriteLine(CommandLineParser<Args>.Usage);
                return;
            }

            // tmx
            XmlDocument docTmx = new XmlDocument();
            docTmx.Load(a.TmxFile);
            TmxImage original = TmxReader.Parse(docTmx);

            // excluded list
            XmlDocument docExcluded = new XmlDocument();
            docExcluded.Load(a.ExcludeFile);
            ExcludeContextCollection excludeList = ExcludeContextCollection.Parse(docExcluded.DocumentElement);

            // languages
            TmxLanguageCollection languages = TmxLanguageCollection.Parse(a.ImportLanguages);

            // solution
            solution = SolutionParser.Parse(a.Solution);

            // Do Merge
            // tmx snapshot
            Dictionary<string, TmxItem> byContext = new Dictionary<string, TmxItem>();
            foreach (TmxItem tmxItem in original.Items)
                byContext[tmxItem.Context] = tmxItem;

            ResXItemCollection itemCollection = new ResXItemCollection();
            foreach(Project p in solution.Projects)
                itemCollection.AddRange(p.ResXList);

            ResXBrowser browser = new ResXBrowser(itemCollection);
            string[] globalKeys = browser.GlobalKeys;
            foreach (string gk in globalKeys)
            {
                ResXItem source = browser.GetLocalization(gk, String.Empty);
                if (source != null)
                {
                    TmxItem tmxItem;
                    if (byContext.TryGetValue(gk, out tmxItem))
                    {
                        foreach (TmxLanguage lang in languages)
                        {
                            string newValue = tmxItem.Values[lang.CultureName];
                            if (newValue != null)
                            {
                                itemCollection[source.ResX.Project, source.ResX.Key, lang.CultureName, source.Key] = newValue;
                            }
                        }
                    }
                }
            }

            List<string> mergeLog = new List<string>();
            foreach (Project p in solution.Projects)
            {
                p.PreviewMerge(mergeLog);
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ResDiffForm form = new ResDiffForm(solution, a.TmxFile, mergeLog, languages);
            form.Merge += new ResDiffForm.MergeHandler(form_Merge);
            Application.Run(form);
        }

        private static Solution solution;


        static void form_Merge(Project.ReadonlyPolicy readonlyPolicy)
        {
            foreach(Project p in solution.Projects)
            {
                p.MergeChanges();
            }
            
        }
    }

    class Args
    {
        [Description("solution which could be integrated into")]
        public string Solution;

        [Description("tmx file which contains translation")]
        public string TmxFile;

        [Description("langages to import, for example en;ru")]
        public string ImportLanguages;

        [Description("language independent resources file")]
        public string ExcludeFile;
    }
}
